﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace ldc_mvc_closing_hwk.Models.Database
{
    public partial class LdcFASContext : DbContext
    {
        public LdcFASContext()
        {
        }

        public LdcFASContext(DbContextOptions<LdcFASContext> options)
            : base(options)
        {
        }

        public virtual DbSet<ArticleArea> ArticleAreas { get; set; }
        public virtual DbSet<ArticleContent> ArticleContents { get; set; }
        public virtual DbSet<ArticleMetum> ArticleMeta { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#if DEBUG
                optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=LdcFAS;Integrated Security=SSPI");
#else
                optionsBuilder.UseSqlServer("Password=114514;Persist Security Info=True;User ID=sa;Initial Catalog=LdcFAS;Data Source=202.189.5.24, 43913");
#endif
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<ArticleArea>(entity =>
            {
                entity.ToTable("ArticleArea");

                entity.HasIndex(e => e.AreaName, "IX_ArticleArea")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.AreaName)
                    .IsRequired()
                    .HasMaxLength(128)
                    .IsUnicode(false)
                    .HasColumnName("area_name")
                    .HasComment("标识名, 为全ascill字符组成, 显示名需在代码中进行翻译");
            });

            modelBuilder.Entity<ArticleContent>(entity =>
            {
                entity.ToTable("ArticleContent");

                entity.HasIndex(e => e.FkArticleMeta, "IX_ArticleContent_1")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.ContentType)
                    .IsRequired()
                    .HasMaxLength(32)
                    .IsUnicode(false)
                    .HasColumnName("content_type")
                    .HasDefaultValueSql("('text')");

                entity.Property(e => e.Data)
                    .IsRequired()
                    .HasColumnName("data")
                    .HasDefaultValueSql("('')");

                entity.Property(e => e.FkArticleMeta)
                    .HasColumnName("FK_article_meta")
                    .HasComment("绑定到的");

                entity.HasOne(d => d.FkArticleMetaNavigation)
                    .WithOne(p => p.ArticleContent)
                    .HasForeignKey<ArticleContent>(d => d.FkArticleMeta)
                    .HasConstraintName("FK_ArticleContent_ArticleMeta");
            });

            modelBuilder.Entity<ArticleMetum>(entity =>
            {
                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.CoverImage)
                    .HasMaxLength(64)
                    .HasColumnName("cover_image")
                    .HasComment("封面图");

                entity.Property(e => e.FkAreaId).HasColumnName("FK_area_id");

                entity.Property(e => e.ReleaseTime)
                    .HasColumnType("datetime")
                    .HasColumnName("release_time")
                    .HasComment("文章发布时间, 为空时未知");

                entity.Property(e => e.SourceName)
                    .HasMaxLength(64)
                    .HasColumnName("source_name")
                    .HasComment("文章来源名称, 为空时未知来源");

                entity.Property(e => e.Summary)
                    .HasMaxLength(256)
                    .HasColumnName("summary")
                    .HasComment("文章概要, 允许为空, 前端应在概要为空时进行处理");

                entity.Property(e => e.Title)
                    .IsRequired()
                    .HasMaxLength(128)
                    .HasColumnName("title")
                    .HasComment("文章标题");

                entity.HasOne(d => d.FkArea)
                    .WithMany(p => p.ArticleMeta)
                    .HasForeignKey(d => d.FkAreaId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ArticleMeta_ArticleArea");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
